<?php
namespace plugins\goods\interfaceLib;
use plugins\goods\controller\GoodsCatsController;
use think\Db;
use cmf\controller\PluginAdminBaseController;//引用插件基类


// use plugins\goods\interfaceLib\GoodsClassify;  //引用分类    
// $spec = new GoodsClassify();   
// $res = $spec->cats($param['catid']);    //分类创建规格方法

/**
 * 分类控制类
 */
class GoodsClassify extends PluginAdminBaseController

{
    

    /**
     * 基础调分类选项
     */
    public function cats($name,$catid)
    {
       
        $goodscats = new GoodsCatsController();
        $cats = $goodscats->catslist();
        $recently = db('goods_cats')->where('recently',1)->find();

        if( !empty($catid) ){
            $pidname = $this->pidname($catid);
            $this->assign('pidname', $pidname);
            $this->assign( 'catid' , $catid );
        } 

        $this->assign( 'name' , $name );
        $this->assign( 'cats' , $cats );
        $this->assign('recently',$recently);
        return $this->fetch('goods_cats/cats');
    } 
    

    /**
     * 分类搜索
     */
    public function catSearch($name,$catid)
    {
        $goodscats = new GoodsCatsController();
        $cats = $goodscats->catslist();
        $this->assign( 'name' , $name );
        $this->assign( 'cats' , $cats );
        $this->assign( 'catid' , $catid );
        return $this->fetch('goods_cats/search');
    }





    /**
     * 基础添加商品
     */
    public function goosdcats($catid)
    {
        
        db('goods_cats')->setField('recently',0);
        db('goods_cats')->where('catid',$catid)->setField('recently', 1);
        
    }


    /**
     * 基础调指定分类的所以上级名称
     */
    public function pidname($catid)
    {
        $data = db('goods_cats')->select();

        //查询名称及上级id
        $cat = db('goods_cats')->where('catid',$catid)->find();
        $catname = $cat['catname'];
        while ($cat['pid'] > 0)
        {
            $cat = db('goods_cats')->where('catid',$cat['pid'])->find();
            $catname = $cat['catname'].' > '.$catname;
        }


        // foreach( $data as $key){
        
        //     //查询名称及上级id
        //     $cat = db('goods_cats')->where('catid',$catid)->find();
        //     $catname = empty($catname) ? $cat['catname'] : $cat['catname'].'>'.$catname;
        //     //如果无上级 结束遍历
        //     if($cat['pid'] == 0)break;
        //     //拼接分类名称
        //     if( $key['catid'] == $cat['pid'] )
        //     {
        //         $catid = $cat['pid'];
        //         $catname = $key['catname'].' > '.$catname;
        //     }
                
        // }

        return $catname;
    } 




}